<?php
/*
Plugin Name: CMP Widget customer
Plugin URI: http://bizwebsky.com
Description: Widget hiện thị nhân viên hỗ trợ
Author: Phương CM
Version: 1.0
Author URI: http://google.com
*/

add_action('widgets_init', 'cmp_wg_customer');
function cmp_wg_customer(){
	register_widget('Cmp_wg_customer');//class bên dưới
}

class Cmp_wg_customer extends WP_Widget {

	function Cmp_wg_customer() {
		$wget_options = array(
			'classname' => 'cmp_wg_customer',
			'description' => 'Widget hiển thị ý kiến khách hàng'
		);
		$this->WP_Widget('cmp_wg_customer_id', 'CMP Widget customer', $wget_options);
	}

	function form($instance)
	{
		$args = array('content' => '', //ý kiến khách hàng
									'name' => '',
			            'website' => '',
									'avatar' => '');
		$instance = wp_parse_args((array) $instance, $args);
		$title = esc_attr($instance['title']);

		$content = 'content';
		$name    = 'name';
		$website = 'website';
		$avatar  = 'avatar';

		$instance[$name]    = $instance[$name];
		$instance[$content] = $instance[$content];
		$instance[$website] = esc_url($instance[$website]);
		$instance[$avatar]  = esc_url($instance[$avatar]);

		?>
		<p>
			<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'bizwebsky'); ?></label>
			<input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>"
			       type="text" value="<?php echo $title; ?>"/>
		</p>

		<p>
			<label
				for="<?php echo $this->get_field_id($name); ?>"> <?php _e('Name ', 'bizwebsky'); ?></label>
			<input type="text" class="widefat" id="<?php echo $this->get_field_id($name); ?>"
			       name="<?php echo $this->get_field_name($name); ?>" value="<?php echo $instance[$name]; ?>"/>
		</p>
		<p>
			<label
				for="<?php echo $this->get_field_id($website); ?>"> <?php _e('Website ', 'bizwebsky'); ?></label>
			<input type="text" class="widefat" id="<?php echo $this->get_field_id($website); ?>"
			       name="<?php echo $this->get_field_name($website); ?>" value="<?php echo $instance[$website]; ?>"/>
		</p>
		<p>
			<label
				for="<?php echo $this->get_field_id($content); ?>"> <?php _e('Ý kiến khách hàng ', 'bizwebsky'); ?></label>
			<input type="text" class="widefat" id="<?php echo $this->get_field_id($content); ?>"
			       name="<?php echo $this->get_field_name($content); ?>" value="<?php echo $instance[$content]; ?>"/>
		</p>

		<p>
			<label
				for="<?php echo $this->get_field_id($avatar); ?>"> <?php _e('Avatar (kích thước tối thiểu 165x165)', 'bizwebsky'); ?></label>

			<?php
			if($instance[$avatar] != '') :
				echo '<img id="'.$this->get_field_id($instance[$avatar].'preview').'"src="'.$instance[$avatar].'"style="max-width:166px; max-height: 166px;" /><br />';
			endif;
			?>

			<input type="text" class="widefat custom_media_url" id="<?php echo $this->get_field_id($avatar); ?>"
			       name="<?php echo $this->get_field_name($avatar); ?>" value="<?php echo $instance[$avatar]; ?>"
			       style="margin-top:5px;"/>

			<input type="button" class="button button-primary custom_media_button" id="custom_media_button"
			       name="<?php echo $this->get_field_name($avatar); ?>" value="<?php _e('Upload Image', 'bizwebsky'); ?>"
			       style="margin-top:5px; margin-right: 30px;"
			       onclick="imageWidget.uploader( '<?php echo $this->get_field_id($avatar); ?>' ); return false;"/>
		</p>

	<?php
	}

	function update($new_instance, $old_instance)
	{
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);

		$name = 'name';
		$content = 'content';
		$website = 'website';
		$avatar = 'avatar';


		$instance[$name] = $new_instance[$name];
		$instance[$content] = $new_instance[$content];
		$instance[$website] = esc_url_raw($new_instance[$website]);
		$instance[$avatar] = esc_url_raw($new_instance[$avatar]);

		return $instance;
	}

	function widget($args, $instance)
	{
		extract($args);
		extract($instance);

		$title = isset($instance['title']) ? $instance['title'] : '';

		$name = 'name';
		$content = 'content';
		$website = 'website';
		$avatar = 'avatar';

		$instance[$name] = isset($instance[$name]) ? $instance[$name] : '';
		$instance[$content] = isset($instance[$content]) ? $instance[$content] : '';
		$instance[$website] = isset($instance[$website]) ? $instance[$website] : '';
		$instance[$avatar] = isset($instance[$avatar]) ? $instance[$avatar] : '';

//		echo $before_widget;
		ob_start();
		?>

		<div class="item clearfix">
			<div class="feature"><img src="<?php echo $instance[$avatar]; ?>" width="165" height="165" style="max-width: 165px;" alt="customer" /></div>
			<p class="text"><?php echo $instance[$content]; ?></p>
			<p class="author"><?php echo $instance[$name]; ?>  - <a href="<?php echo $instance[$website]; ?>" class="link"><?php echo $instance[$website]; ?></a></p>
		</div>

		<?php
		$content = ob_get_contents();
		ob_clean();
		echo $content;
	}

}
